#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    m_pCapture = new CapThreadV4L2(NULL);
    if(m_pCapture)
    {
        m_pCapture->SetChannel(CHN_VISIBLE);
        m_pCapture->start();
    }

    m_pEncH264 = new MediaEncHD(NULL,true);
    if(m_pEncH264)
        m_pEncH264->start();

    connect(m_pCapture,SIGNAL(AlgProcFrame(stVFrame*)),m_pEncH264,SLOT(ProcessFrame(stVFrame*)));

    m_pOneSecond = new QTimer(this);
    connect(m_pOneSecond, SIGNAL(timeout()), this, SLOT(OnHandleOneSecond()));
    m_pOneSecond->start(1000);

}

MainWindow::~MainWindow()
{


    delete ui;
    if(m_pCapture)
    {
        m_pCapture->Stop();
        m_pCapture->wait();
        delete (m_pCapture);(m_pCapture)=NULL;
    }

    if(m_pEncH264)
    {
        m_pEncH264->Stop();
        m_pEncH264->wait();
        delete (m_pEncH264);(m_pEncH264)=NULL;
    }
}

void MainWindow::OnHandleOneSecond()
{
    int nRtspFps = 0;
    int nVisFps = m_pCapture->GetFps_tv();
    int nEncFps = m_pEncH264->GetFps();
    char  cMsgBuff[STR_BUFF_LEN] = {0};
    sprintf(cMsgBuff,"cap tv[%3d] enc[%3d]",nVisFps,nEncFps);
    log_i("%s",cMsgBuff);
}
